package com.ilop.sthome.ui.adapter.detail;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.common.utils.LiveDataBus;
import com.ilop.sthome.common.CommonId;
import com.ilop.sthome.data.greenDao.AutomationBean;
import com.ilop.sthome.utils.CoderUtils;
import com.siterwell.familywellplus.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author skygge
 * @date 2020-04-03.
 * GitHub：javofxu@github.com
 * email：skygge@yeah.net
 * description：插座定时
 */
public class TimingAdapter extends RecyclerView.Adapter<TimingAdapter.ItemHolder> {

    private Context mContext;
    private boolean isSocket = true;
    private List<AutomationBean> mAutomationList;

    public TimingAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void setAutomationList(List<AutomationBean> mAutomationList) {
        this.mAutomationList = mAutomationList;
        notifyDataSetChanged();
    }

    public void setSocket(boolean socket) {
        isSocket = socket;
    }

    @NonNull
    @Override
    public ItemHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View mView = LayoutInflater.from(mContext).inflate(R.layout.item_socket_timing, viewGroup, false);
        return new ItemHolder(mView);
    }

    @Override
    public void onBindViewHolder(@NonNull ItemHolder itemHolder, int i) {
        AutomationBean bean = mAutomationList.get(i);
        String inputCode = bean.getCode().substring(26, 34);
        String init_week = inputCode.substring(2, 4);
        String week = CoderUtils.getWeekInfo(mContext, init_week);
        int hour = Integer.parseInt(inputCode.substring(4, 6), 16);
        int min = Integer.parseInt(inputCode.substring(6), 16);
        String time = CoderUtils.setCodeSupplement1Byte(String.valueOf(hour))
                + ":"+ CoderUtils.setCodeSupplement1Byte(String.valueOf(min)) ;
        itemHolder.itemWeek.setText(week);
        itemHolder.itemTime.setText(time);
        String state;
        if (isSocket) {
            String outputCode = bean.getCode().substring(46, 48);
            switch (outputCode) {
                case "20":
                    state = mContext.getString(R.string.close);
                    break;
                case "30":
                    state = mContext.getString(R.string.open);
                    break;
                case "40":
                    state = mContext.getString(R.string.socket_switch);
                    break;
                default:
                    state = mContext.getString(R.string.close);
                    break;
            }
        }else {
            String outputCode = bean.getCode().substring(48, 50);
            int code = Integer.parseInt(outputCode, 16);
            if (code>30){
                int temp = code - 32;
                state =  temp + "." + "5℃";
            }else {
                state = code + "℃";
            }
        }
        itemHolder.itemState.setText(state);
        itemHolder.itemView.setOnClickListener(view -> LiveDataBus.get().with(CommonId.TIMING_ON_CLICK).setValue(bean.getMid()));
    }

    @Override
    public int getItemCount() {
        return mAutomationList == null ? 0 : mAutomationList.size();
    }

    static class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_time)
        TextView itemTime;
        @BindView(R.id.item_state)
        TextView itemState;
        @BindView(R.id.item_week)
        TextView itemWeek;
        ItemHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
